package com.caojing.syby.ui.details.searchdetails;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.caojing.syby.R;
import com.caojing.syby.base.BaseActivity;
import com.caojing.syby.bean.home.HomeGvBean;
import com.caojing.syby.bean.home.RowsBean;
import com.caojing.syby.config.UrlConfig;
import com.caojing.syby.ui.adapter.HomeGvAdapter;
import com.caojing.syby.ui.details.productdetail.ClickGridviewItemActivity;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class SearchDetailActivity extends BaseActivity implements SearchDetailContract.View {


    @Bind(R.id.search_activity_detail_tvTitle)
    TextView searchActivityDetailTvTitle;
    @Bind(R.id.search_activity_detail_tvSelect)
    TextView searchActivityDetailTvSelect;
    @Bind(R.id.search_activity_detail_btnDefault)
    Button searchActivityDetailBtnDefault;
    @Bind(R.id.search_activity_detail_btnSales)
    Button searchActivityDetailBtnSales;
    @Bind(R.id.search_activity_detail_btnPrice)
    Button searchActivityDetailBtnPrice;
    @Bind(R.id.search_activity_detail_btnNew)
    Button searchActivityDetailBtnNew;
    @Bind(R.id.search_activity_detail_gv)
    PullToRefreshGridView searchActivityDetailGv;
    private SearchDetailContract.Presenter presenter;
    private String[] mBc = new String[]{"1", "2", "3", "4", "5", "1", "7", "8", "9", "10", "1", "6", "0", "0", "0"};;
    private String[] mSc= new String[]{"0", "0", "0", "0", "0", "13", "0", "0", "0", "0", "16", "0", "0", "0", "0"};;
    private String name;
    private int sort;
    private int index = 0;
    private int PAGE = 1;
    private List<RowsBean> totallist = new ArrayList<>();
    private HomeGvAdapter adapter;
    private HashMap<String,String> params;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_detail;
    }

    @Override
    protected void initView() {
        presenter = new SearchDetailPresenter(this);
        searchActivityDetailBtnDefault.setTextColor(Color.rgb(254, 68, 129));
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        searchActivityDetailTvTitle.setText(name);
        sort = intent.getIntExtra("sort", 0);

        searchActivityDetailGv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                searchActivityDetailGv.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                searchActivityDetailGv.onRefreshComplete();
            }
        });
    }

    @Override
    protected void initData() {
        initParams();
        presenter.initGvData(params);
    }

    private HashMap<String,String> initParams() {
        params = new HashMap<>();
        params.put(UrlConfig.Key.BC, mBc[sort]);
        params.put(UrlConfig.Key.SORTS,"");
        params.put(UrlConfig.Key.ACT, UrlConfig.DefaultValue.ACT);
        params.put(UrlConfig.Key.V, UrlConfig.DefaultValue.V);
        params.put(UrlConfig.Key.PAGES, PAGE+"");
        params.put(UrlConfig.Key.SC, mSc[sort]);
        if(sort==12){
            params.put(UrlConfig.Key.CHANNEL, "3");
        }else {
            params.put(UrlConfig.Key.CHANNEL, "0");
        }
        params.put(UrlConfig.Key.CKEY, UrlConfig.DefaultValue.CKEY);
        if(sort==14){
            params.put(UrlConfig.Key.DAYNEWS, "");
        }else {
            params.put(UrlConfig.Key.DAYNEWS, "1");
        }
        if(sort==13){
            params.put(UrlConfig.Key.DAYNEWS,"1");
        }else {
            params.put(UrlConfig.Key.DAYNEWS,"");
        }
        return params;
    }

    @Override
    public void onSuccessGetGv(HomeGvBean homeGvBean) {
        List<RowsBean> list = homeGvBean.getRows();
        adapter = new HomeGvAdapter(list,this);
        searchActivityDetailGv.setAdapter(adapter);
        searchActivityDetailGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(SearchDetailActivity.this, ClickGridviewItemActivity.class);
                RowsBean rowsBean = totallist.get(position);
                if (rowsBean != null) {
                    String spreadUrl = rowsBean.getSpreadUrl();
                    intent.putExtra("spreadUrl", spreadUrl);
                    String name = rowsBean.getName();
                    intent.putExtra("name",name);
                    startActivity(intent);
                }
            }
        });

    }

    @Override
    public void onFailGetGv(String msg) {
        Toast.makeText(SearchDetailActivity.this,msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.search_activity_detail_tvTitle, R.id.search_activity_detail_tvSelect, R.id.search_activity_detail_btnDefault, R.id.search_activity_detail_btnSales, R.id.search_activity_detail_btnPrice, R.id.search_activity_detail_btnNew})
    public void onClick(View view) {
        showColor();
        initParams();
        switch (view.getId()) {
            case R.id.search_activity_detail_tvTitle:
                break;
            case R.id.search_activity_detail_tvSelect:
                break;
            case R.id.search_activity_detail_btnDefault:
                index = 0;
                searchActivityDetailBtnDefault.setTextColor(Color.rgb(254, 68, 129));
                params.put(UrlConfig.Key.SORTS,"");
                break;
            case R.id.search_activity_detail_btnSales:
                index = 0;
                searchActivityDetailBtnSales.setTextColor(Color.rgb(254, 68, 129));
                params.put(UrlConfig.Key.SORTS,"sale2l");
                break;
            case R.id.search_activity_detail_btnPrice:
                searchActivityDetailBtnPrice.setTextColor(Color.rgb(254, 68, 129));
                if (index % 2 == 0) {
                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_sort_up);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    searchActivityDetailBtnPrice.setCompoundDrawables(null, null, drawable, null);
                    params.put(UrlConfig.Key.SORTS,"pl2h");
                } else {
                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_sort_down);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    searchActivityDetailBtnPrice.setCompoundDrawables(null, null, drawable, null);
                    params.put(UrlConfig.Key.SORTS,"ph2l");
                }
                index++;
                break;
            case R.id.search_activity_detail_btnNew:
                index = 0;
                searchActivityDetailBtnNew.setTextColor(Color.rgb(254, 68, 129));
                params.put(UrlConfig.Key.SORTS,"new");
                break;
        }
        presenter.initGvData(params);
    }
    private void showColor() {
        searchActivityDetailBtnDefault.setTextColor(Color.BLACK);
        searchActivityDetailBtnSales.setTextColor(Color.BLACK);
        searchActivityDetailBtnPrice.setTextColor(Color.BLACK);
        searchActivityDetailBtnNew.setTextColor(Color.BLACK);
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_sort_unselected);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        searchActivityDetailBtnPrice.setCompoundDrawables(null, null, drawable, null);
    }
}
